﻿#region Opis pliku
//-----------------------------------------------------------------------------
// Tank.cs
// Autor: Jan Fraś
// 30.10.2012
//-----------------------------------------------------------------------------
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace pogra
{
    /// <summary>
    /// klasa czołgu.
    /// na czołg składają się cztery osłony (przód, tył i boki) oraz wieżyczka.
    /// czołg ponosi jakiekolwiek obrażenia dopiero po zniszczeniu osłony z danej strony.
    /// </summary>
    class Tank : Vehicles
    {
        #region pola
        Turret turret;
        Armor[] armor = new Armor[4];
        //Texture2D particletexture;

        #endregion

        #region inicjalizacja

        public Tank(Vector2 pos, float rotation, int team, Texture2D texture, Texture2D particletexture, List<Unit> enemyteam, float velocity = 0) : 
            base(pos,rotation,team, enemyteam)
        {
            hp = 1000;
            maxhp = hp;
            attackPower = 30;
            upgradeQuant = 5;
            int offx = 60;
            int offy = 36;
            for (int i = 0; i < 4; i++)
                armor[i] = new Armor(texture,this,  new Rectangle((i + 1) * offx, 0, offx, offy),Rand.nextFloat(0.8,0.83));
            turret = new Turret(texture, this, new Rectangle(0, 0, offx, offy));
            this.team = team;
            range = 300;
            shotrange = 500;
            this.velocity = velocity;
            this.particletexture = particletexture;
            setTurret();
        }
        #endregion

        #region różna logika

        /// <summary>
        /// ustawienie wieżyczki na cel
        /// </summary>
        private void setTurret()
        {
            if (enemy != null)
                turret.Rotation = (float)((Math.Atan2(enemy.Position.Y - position.Y, enemy.Position.X - position.X) + Math.PI * 2) % (Math.PI * 2));
            else
                turret.Rotation = rotation;
        }

        /// <summary>
        /// metoda ataku
        /// </summary>
        /// <returns></returns>
        protected override bool attack()
        {
            setTurret();
            if(base.attack())
            {
                particleSystem.AddEmitter(new TankShotEmitter2(position + new Vector2((float)(30 * Math.Cos(turret.Rotation)), (float)(30 * Math.Sin(turret.Rotation))), particletexture, turret.Rotation));
                lastshottime = lifetime;
                Bullet tmp = new Bullet(attackPower, turret.Rotation, enemy, this, particleSystem);
                bulletmanager.add(tmp);
            }
            return  base.attack();
        }

        /// <summary>
        /// przyjęcie obrażeń
        /// </summary>
        /// <param name="hit"></param>
        /// <param name="attackrotation"></param>
        /// <returns></returns>
        public override int takehit(int hit, float attackrotation)
        {
            particleSystem.AddEmitter(new HitEmitter(position - new Vector2((float)(20 * Math.Cos(attackrotation)), (float)(20 * Math.Sin(attackrotation))), attackrotation));

            int tmphit = 0;
            switch ((int)((-rotation + attackrotation - 3 * Math.PI / 4) * 2 / Math.PI + 4) % 4)
            {
                case 0:
                    tmphit = armor[2].takehit(hit);
                    break;

                case 1:
                    tmphit = armor[1].takehit(hit);
                    break;

                case 2:
                    tmphit = armor[3].takehit(hit);
                    break;

                case 3:
                    tmphit = armor[0].takehit(hit);
                    break;
            }

            return base.takehit(tmphit, attackrotation);
        }

        #endregion

        #region metody stanów
        protected override void IdleState(TimeSpan deltat)
        {
            if (findtarget())
                State = UnitState.Attacking;
            base.IdleState(deltat);
        }

        protected override void DeadState(TimeSpan deltat)
        {

            if (flags.BeingDaed == false)
            {
                particleSystem.AddEmitter(new ExplosionEmitter(position));
            }
            base.DeadState(deltat);
        }

        protected override void TravelingState(TimeSpan deltat)
        {
            setTurret();
            if(enemy!= null)
                attack();

            base.TravelingState(deltat);
        }

        protected override void AttackingState(TimeSpan deltat)
        {
            flags.Attacked = false;

            if (enemy != null)
            {
                if ((position - enemy.Position).Length() > range && mood == Moodes.Aggressive)
                {
                    setvalues();
                    move(deltat);
                }
                else
                {

                }
                attack();
                base.AttackingState(deltat);
            }
            else
                if(findtarget() == false)
                    State = UnitState.Idle;
        }

        
        #endregion

        #region metody draw i update
        public override void Draw(ExtendedSpriteBatch exSpriteBatch)
        {
            foreach (Armor asd in armor)
                asd.Draw(exSpriteBatch, color);

            turret.Draw(exSpriteBatch);
            base.Draw(exSpriteBatch);
        }

        public override bool Update(TimeSpan deltat)
        {


            if (lifetime < 500 && State != UnitState.Dead)
            {
                move(deltat);
                lifetime += deltat.Milliseconds;
                return false;
            }
            base.Update(deltat);
            return true;
        }

        #endregion

    }
}
